package cz.mendelu.swi2.spravaProjektu;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ProjectController {

	private final String LIST_MAPPING = "/spravaProjektu/list";
	private final String LIST_VIEW = "spravaProjektu/list";
	private final String DETAIL_MAPPING = "/spravaProjektu/detail";
	private final String DETAIL_VIEW = "spravaProjektu/detail";
	private final String CREATE_MAPPING = "/spravaProjektu/create";
	private final String CREATE_VIEW = "spravaProjektu/create";
	private final String EDIT_MAPPING = "/spravaProjektu/edit";
	private final String EDIT_VIEW = "spravaProjektu/edit";
	private final String REDIRECT_TO_DEFAULT_PAGE = "redirect:/forms" + LIST_MAPPING;
	
	//@Autowired
	private ProjectManager projectManager;
	private SVNManager svnManager;
	
	public ProjectController() {
		projectManager = new ProjectManager();
		svnManager = new SVNManager();
	}
	
	/**
	 * Show list of projects.
	 * @param model
	 * @return
	 */
	@RequestMapping(value = LIST_MAPPING, method = RequestMethod.GET)
	public String list(ModelMap model) {
		List<Project> projects = projectManager.find();
		model.addAttribute("projects", projects);
		return LIST_VIEW;
	}
	
	/**
	 * Show detail of selected project.
	 * @param projectId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = DETAIL_MAPPING, method = RequestMethod.GET)
	public String detail(@RequestParam("id") Long projectId, ModelMap model) {
		Project project = projectManager.findById(projectId);
		model.addAttribute("project", project);
		return DETAIL_VIEW;
	}
	
	/**
	 * Show form for create new project.
	 * @param model
	 * @return
	 */
	@RequestMapping(value = CREATE_MAPPING, method = RequestMethod.GET)
	public String create(ModelMap model) {
        model.addAttribute("project", new Project());
        model.addAttribute("projectStates", ProjectState.values());
		return CREATE_VIEW;
	}
	
	/**
	 * Handles form for create project after submit.
	 * @param createRepo
	 * @param model
	 * @param project
	 * @param result
	 * @return
	 */
	@RequestMapping(value = CREATE_MAPPING, method = RequestMethod.POST)
	public String createSubmit(@RequestParam(value = "createRepository", required = false) boolean createRepo, ModelMap model, @ModelAttribute("project") Project project,
			BindingResult result) {
		if (result.hasErrors()) {
			return CREATE_VIEW;
		}
		if (createRepo) {
			svnManager.createSVNRepository(project);
		}
		projectManager.create(project);
		return REDIRECT_TO_DEFAULT_PAGE;
	}
	
	/**
	 * Show form for edit existing project and fill fields according to project data.
	 * @param projectId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = EDIT_MAPPING, method = RequestMethod.GET)
	public String edit(@RequestParam("id") Long projectId, ModelMap model) {
		Project project = projectManager.findById(projectId);
		model.addAttribute("project", project);
		model.addAttribute("projectStates", ProjectState.values());
		return EDIT_VIEW;
	}
	
	/**
	 * Handles form for edit project after its submit.
	 * @param createRepo Value from checkbox for creating repository.
	 * @param model
	 * @param project
	 * @param result
	 * @return
	 */
	@RequestMapping(value = EDIT_MAPPING, method = RequestMethod.POST)
	public String editSubmit(@RequestParam(value = "createRepository", required = false) boolean createRepo, ModelMap model, @ModelAttribute("project") Project project,
			BindingResult result) {
		if (result.hasErrors()) {
			return EDIT_VIEW;
		}
		if (createRepo) {
			svnManager.createSVNRepository(project);
		}
		svnManager.editPermissions(project);
		projectManager.edit(project);
		return REDIRECT_TO_DEFAULT_PAGE;
	}
}
